<script setup>
import { ref, onMounted } from "vue";
import { Edit, Delete } from '@element-plus/icons-vue'
import { artGetListService, artDelService } from '@/api/article'
import { formatTime } from '@/utils/format'
import ChannelSelect from './components/ChannelSelect.vue'
import ArticleEdit from './components/ArticleEdit.vue'

onMounted(() => {
  getArticleList()
})


const articleEditRef = ref()
// 新增逻辑
const onAddArticle = () => {
  //通过ref调用子组件暴漏的方法open,不传值代表新增文章
  articleEditRef.value.open({})
}
// 编辑逻辑
const onEditArticle = (row) => {
  //传值代表编辑文章
  articleEditRef.value.open(row)
}

//删除文章
const onDeleteArticle = async (row) => {
  await ElMessageBox.confirm('你确认删除该文章信息吗？', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  await artDelService(row.id)
  ElMessage({ type: 'success', message: '删除成功' })
  getArticleList()
}
const total = ref(0)
const articleList = ref([
])
const params = ref({
  pagenum: 1,
  pagesize: 5,
  cate_id: '',
  state: ''
})
//获取文章列表
const getArticleList = async () => {
  loading.value = true
  const res = await artGetListService(params.value)
  console.log(res.data.data)
  articleList.value = res.data.data
  total.value = res.data.total
  loading.value = false
}
//getArticleList()
const onSizeChange = (size) => {
  params.value.pagenum = 1
  params.value.pagesize = size
  getArticleList()
}
const onCurrentChange = (page) => {
  params.value.pagenum = page
  getArticleList()
}
const loading = ref(false)
//搜索按钮
const onSearch = () => {
  params.value.pagenum = 1
  getArticleList()
}
//重置按钮
const onReset = () => {
  params.value.pagenum = 1
  params.value.cate_id = ''
  params.value.state = ''
  getArticleList()
}
// 添加修改成功
const onSuccess = (type) => {
  if (type === 'add') {
    // 如果是添加，需要跳转渲染最后一页，编辑直接渲染当前页
    const lastPage = Math.ceil((total.value + 1) / params.value.pagesize)
    params.value.pagenum = lastPage
  }
  getArticleList()
}
</script>
<template>
  <page-container title="文章管理">
    <template #extra>
      <el-button type="primary" @click="onAddArticle()">发布文章</el-button>
    </template>
    <!-- 表单部分 -->
    <el-form inline>
      <el-form-item label="文章分类" class="flex-item">
        <channel-select v-model="params.cate_id" :style="{ width: '180px' }"></channel-select>

      </el-form-item>
      <el-form-item label="发布状态" class="flex-item" style="width: 240px">
        <el-select v-model="params.state">
          <el-option label="已发布" value="已发布"></el-option>
          <el-option label="草稿" value="草稿"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item class="flex-item">
        <!-- 搜索和重置 -->
        <el-button type="primary" @click="onSearch">搜索</el-button>
        <el-button @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格部分 -->
    <el-table v-loading="loading" :data="articleList" style="width: 100%">
      <el-table-column label="序号" width="100" type="index"> </el-table-column>
      <el-table-column label="文章标题" prop="title">
        <template #default="{ row }">
          <el-link type="primary" :underline="false">{{ row.title }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="文章分类" prop="categoryName"></el-table-column>
      <el-table-column label="发布时间" prop="publishTime">
        <template #default="{ row }">
          {{ formatTime(row.pub_date) }}
        </template>
      </el-table-column>
      <el-table-column label="发布状态" prop="state"></el-table-column>
      <el-table-column label="操作" width="100">
        <template #default="{ row }">
          <el-button :icon="Edit" circle plain type="primary" @click="onEditArticle(row)"></el-button>
          <el-button :icon="Delete" circle plain type="danger" @click="onDeleteArticle(row)"></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
    <article-edit ref="articleEditRef" @success="onSuccess"></article-edit>
    <!-- 分页部分 -->
    <el-pagination v-model:current-page="params.pagenum" v-model:page-size="params.pagesize"
      :page-sizes="[2, 3, 4, 5, 10]" layout="jumper, total, sizes, prev, pager, next" background :total="total"
      @size-change="onSizeChange" @current-change="onCurrentChange"
      style="margin-top: 20px; justify-content: flex-end" />
    <!-- 抽屉部分 -->
    <!-- <el-drawer v-model="visibleDrawer" title="编辑文章" size="50%" direction="ltr">
      <span>抽屉出来了</span>
    </el-drawer> -->
  </page-container>
</template>



<style lang="scss" scoped></style>
